﻿using System;
using System.Xml.Linq;
using System.IO;
using EnvDTE;
using UtfprLibrary.Exceptions;

namespace UtfprLibrary.Configurations.DesignTime {
    /// <summary>
    /// TODO
    /// </summary>
    public class UtfprSectionBase {
        #region private

        /// <summary>
        /// TODO
        /// </summary>
        /// <exception cref="UtfprLibrary.Exceptions.UtfprConfigurationsException">Thrown when an exception occurs.</exception>
        private static string getActiveProjectAppConfigPath() {
            try {
                return GetActiveProjectDirectoryPath() + "\\App.config";
            } catch (Exception ex) {
                throw new UtfprConfigurationsException("Could not get the active project directory path.", ex);
            }     
        }

        #endregion

        /// <summary>
        /// TODO
        /// </summary>
        /// <exception cref="UtfprLibrary.Exceptions.UtfprConfigurationsException">Thrown when an exception occurs.</exception>
        protected static string GetActiveProjectPath() {
            try {
                DTE dte = (DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
                Array activeSolutionProjects = dte.ActiveSolutionProjects as Array;
                if (activeSolutionProjects != null && activeSolutionProjects.Length > 0) {
                    Project activeProject = activeSolutionProjects.GetValue(0) as EnvDTE.Project;
                    return activeProject.FullName;
                }
                throw new ArgumentException("There is no active solution projects.");
            } catch (Exception ex) {
                throw new UtfprConfigurationsException("Could not get the active project path.", ex);
            }            
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <exception cref="UtfprLibrary.Exceptions.UtfprConfigurationsException">Thrown when an exception occurs.</exception>
        protected static string GetActiveProjectDirectoryPath() {
            try {
                return Path.GetDirectoryName(GetActiveProjectPath());
            } catch (Exception ex) {
                throw new UtfprConfigurationsException("Could not get the active project directory path.", ex);
            }
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <exception cref="UtfprLibrary.Exceptions.UtfprConfigurationsException">Thrown when an exception occurs.</exception>
        protected static XDocument GetAppConfigDocument() {
            try {
                XDocument config = XDocument.Load(getActiveProjectAppConfigPath());

                if (config != null) {
                    return config;
                }

                throw new ArgumentException("Could not find AppConfig document. ");
            } catch (Exception ex) {
                throw new UtfprConfigurationsException("Could not get the AppConfig document.", ex);
            }               
        }
    }
}
